<?php

namespace app\controller;

use app\service\ProjectService;
use support\Request;
use support\Response;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class ProjectController
{
    /**
     * @var ProjectService
     */
    private $projectService;

    public function __construct()
    {
        $this->projectService = ProjectService::getInstance();
    }

    /**
     * 镜头统计列表
     * @param Request $request
     * @return \support\Response
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function shotTotalList(Request $request)
    {
        $param = $request->post();

        $projectId = $param['project_id'] ?? 0;
        $entityName = $param['entity_name'] ?? '';
        $shotStatus = param_to_array($param['shot_status'] ?? '');
        $initialEpisodeCode = param_to_array($param['initial_episode_code'] ?? '');
        $initialEpisodeCodeList = $param['initial_episode_code_list'] ?? [];
        $categoryIdList = param_to_array($param['category_id_list'] ?? '', ',', 'intval');
        $yearMonth = $param['year_month'] ?? 0;
        $storyboardDemandStatus = param_to_array($param['storyboard_demand_status'] ?? '', ',', 'intval');

        $res = $this->projectService->shotTotalList($request, $projectId, $entityName, $shotStatus, $initialEpisodeCode, $initialEpisodeCodeList, $categoryIdList, $yearMonth, $storyboardDemandStatus);
        return json(success_response('', $res));
    }

    /**
     * 资产统计列表
     * @param Request $request
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function assetTotalList(Request $request): Response
    {
        $param = $request->post();
        $categoryCodeMap = $this->projectService->assetTotalList($param, $request->getTenantId());
        $returnData = []; // 初始统计图
        foreach (['场景' => 'role', '角色' => 'scene', '道具' => 'prop', '关卡' => 'level'] as $name => $code) {
            $returnData[] = [
                'grad_list' => ["S+", "S", "A", "B", "C"],
                'grad_num' => [
                    intval($categoryCodeMap[$name]['S+'] ?? 0),
                    intval($categoryCodeMap[$name]['S'] ?? 0),
                    intval($categoryCodeMap[$name]['A'] ?? 0),
                    intval($categoryCodeMap[$name]['B'] ?? 0),
                    intval($categoryCodeMap[$name]['C'] ?? 0)
                ],
                'final_num' => intval($categoryCodeMap[$name]['final_num'] ?? 0),
                'total_num' => intval($categoryCodeMap[$name]['total_num'] ?? 0),
                'category_name' => $name,
                'category_code' => $code,
            ];
        }
        return json(success_response('success', $returnData));
    }


}
